package com.loooooo.pm2024.config;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Locale;

@Configuration
public class JsonConf {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer() {
        return builder -> {
            //objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            /*
             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);//.Inclusion.NON_NULL);

        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        objectMapper.setLocale(Locale.CHINA);
             */
            ObjectMapper objectMapper = new ObjectMapper();
            builder.serializationInclusion(JsonInclude.Include.NON_NULL);
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
            objectMapper.setLocale(Locale.CHINA);

            objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

            builder.configure(objectMapper);
//            // 全局配置序列化返回 JSON 处理
//            JavaTimeModule javaTimeModule = new JavaTimeModule();
//            // Jackson 序列化 Long 类型超出 JS 最大最小值序列化位字符串转为 String，解决后端返回的类型在前端精度丢失的问题
//            javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
//            javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
//            javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
//
//            // Jackson 序列化 BigDecimal 类型为 String，解决后端返回的类型在前端精度丢失的问题
//            javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
//
//            // ======================= 时间序列化规则 =======================
//            // yyyy-MM-dd HH:mm:ss
//            javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DatePattern.NORM_DATETIME_FORMATTER));
//            // yyyy-MM-dd
//            javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE));
//            // HH:mm:ss
//            javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
//            // Instant 类型序列化
//            javaTimeModule.addSerializer(Instant.class, InstantSerializer.INSTANCE);
//
//
//            // ======================= 时间反序列化规则 =======================
//            // yyyy-MM-dd HH:mm:ss
//            javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DatePattern.NORM_DATETIME_FORMATTER));
//            // yyyy-MM-dd
//            javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE));
//            // HH:mm:ss
//            javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME));
//            // Instant 反序列化
//            javaTimeModule.addDeserializer(Instant.class, InstantDeserializer.INSTANT);
//
//            builder.modules(javaTimeModule);
//            // 时区配置
//            builder.timeZone(TimeZone.getDefault());
        };
    }
}
